/// <reference path="jquery-1.4.4-vsdoc.js" />



$(function () {
    $("#userInfoList tr").bind("click", function () {
        var ID = $.trim($(this).children().first().html());
    });
    $("#searchBtn").bind("click", function () {
        $("input#index").val(1);
        $("#curID").html("");
//        $("#curID").val("");
        getMyRoleConfig();
    });
    $("#resetBtn").bind("click", function () {
        $("input#accountID").val("");
        $("input#userName").val("");
        $("select#isAdmin").get(0).selectedIndex = 0;
        $("select#roleID").get(0).selectedIndex = 0;
        $("input#index").val(1);
        $("select#CompanyCollection").get(0).selectedIndex = 0;
    });
    $("tr.AlternatingRowStyle").bind("click", function () {
        //        $(this).attr("style", "BACKGROUND-COLOR:#87CEFF");
        $(this).siblings().children().css("background-color", "#f0f6ff");
        $(this).children().css("background-color", "#87CEFF");
        var accountID = $.trim($(this).children().eq(0).html());
        var curLoginID = $("#curAccID").val();
        $("#curID").html(accountID);
//        $("#curID").val(accountID);
        var roleName = $.trim($(this).children().eq(3).html());
        var isAdmin = $.trim($(this).children().eq(2).html());
        if (isAdmin == "是") {
            $("[name='isAdmin']").attr("checked", true);
        }
        else {
            $("[name='isAdmin']").attr("checked", false);
        }
        $("[name='roleName'] option").each(function () {
            if ($(this).text() == roleName) {
                $(this).attr("selected", "selected");
            }
        });
        if (curLoginID == accountID) {
            $("#updateAdmin").attr('disabled', "disabled");
            $("#roleName").attr('disabled', "disabled");
        }
        else {
            $("#updateAdmin").removeAttr('disabled');
            $("#roleName").removeAttr('disabled');
        }
    });
    $("#updateBtn").bind("click", function () {
        var accountID = $.trim($("#curID").html());
        if (accountID == "") {
            alert("请选择需要修改的配置人员!");
        }
        else {
            $.ajax({
                url: "../ITService/getSingleConfigInfoByAccountID?r=" + (new Date()).toString() + Math.random() * 999,
                type: "GET",
                data: {
                    "accountID": accountID
                },
                dataType: "json",
                success: function (data) {
                    if (data.Data.single.accountID == null) {
                        $.ajax({
                            url: "../ITService/insertRoleInfoBy?r=" + (new Date()).toString() + Math.random() * 999,
                            type: "GET",
                            data: {
                                "accountID": accountID,
                                "isAdmin": $("input#updateAdmin").is(':checked'),
                                "roleID": $("select#roleName option:selected").val()
                            },
                            dataType: "json",
                            success: function (data) {
                                if (data.Data.rowAffected == 1) {
                                    getMyRoleConfig();
                                    alert("恭喜您，操作成功！");
                                }
                            }
                        });
                    }
                    else {
                        $.ajax({
                            url: "../ITService/updateRoleInfoBy?r=" + (new Date()).toString() + Math.random() * 999,
                            type: "GET",
                            data: {
                                "accountID": accountID,
                                "isAdmin": $("input#updateAdmin").is(':checked'),
                                "roleID": $("select#roleName option:selected").val()
                            },
                            dataType: "json",
                            success: function (data) {
                                if (data.Data.rowAffected == 1) {
                                    alert("恭喜您，操作成功！");
                                    getMyRoleConfig();
                                }
                                else if (data.Data.rowAffected == -1) {
                                    alert("操作失败！原因：不能修改当前登录用户信息");
                                }
                            }
                        });
                    }
                }

            });
        }
    });
});
function getMyRoleConfig() {
    var accountID = $("#accountID").val();
    var userName = $("#userName").val();
    var isAdmin = $("#isAdmin option:selected").val();
    var roleID = $("#roleID option:selected").val();
    var ps = parseInt($("#size").val(), 10);
    var pi = parseInt($("#index").val(), 10);
    var company = $("#CompanyCollection option:selected").val();
    $.ajax({
        url: "../ITService/getConfigInfoBy?r=" + (new Date()).toString() + Math.random() * 999,
        type: "GET",
        data: {
            "accountID": accountID,
            "userName": userName,
            "isAdmin": isAdmin,
            "roleID": roleID,
            "company": company,
            "ps": ps,
            "pi": pi
        },
        dataType: "json",
        success: function (data) {
            $("#userInfoList").empty();
            $("#userInfoList").append("<tr><th>账号名</th><th>姓名</th><th>管理员身份</th><th>角色</th><th>地市</th></tr>");
            $.each(data.Data.list, function (i, item) {
                $("<tr id=\"" + item.accountID + "\" class=\"AlternatingRowStyle\"><td>" + item.accountID + "</td><td>" + item.userName + "</td><td>" + item.isAdmin + "</td><td>" + item.roleName+"</td><td>"+item.company+"</td></tr>").insertAfter($("#userInfoList tr:eq(" + i + ")"));
            });

            $("tr.AlternatingRowStyle").bind("click", function () {
                //                $(this).attr("style", "BACKGROUND-COLOR:#87CEFF");
                $(this).siblings().children().css("background-color", "#f0f6ff");
                $(this).children().css("background-color", "#87CEFF");
                var accountID = $.trim($(this).children().eq(0).html());
                //                $("#curID").html(accountID);
                var curLoginID = $("#curAccID").val();
                $("#curID").html(accountID);
                var roleName = $.trim($(this).children().eq(3).html());
                var isAdmin = $.trim($(this).children().eq(2).html());
                if (isAdmin == "是") {
                    $("[name='isAdmin']").attr("checked", true);
                }
                else {
                    $("[name='isAdmin']").attr("checked", false);
                }
                $("[name='roleName'] option").each(function () {
                    if ($(this).text() == roleName) {
                        $(this).attr("selected", "selected");
                    }
                });
                if (curLoginID == accountID) {
                    $("#updateAdmin").attr('disabled', "disabled");
                    $("#roleName").attr('disabled', "disabled");
                }
                else {
                    $("#updateAdmin").removeAttr('disabled');
                    $("#roleName").removeAttr('disabled');
                }
            });
            document.getElementById('itemCount').value = data.Data.count;
            var pager = new PagerView('pager');
            pager.index = parseInt($("input#index").val(), 10);
            pager.itemCount = data.Data.count;
            pager.pageCount = parseInt($("input#pageCount").val(), 10);
            pager.size = parseInt($("input#size").val(), 10);
            pager.onclick = function (index) {
                $("#index").val(index);
                getMyRoleConfig();
            }
            pager.render();
        }
    });
}